package com.food.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.food.dao.service.CollectService;
import com.food.dao.service.OrderService;
import com.food.model.Collect;
import com.food.model.Member;
import com.food.model.Order;

/**
 * Servlet implementation class CollectServlet
 */
@WebServlet("/member/OrderServlet")
public class OrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
		int hismbid = Integer.parseInt(request.getParameter("hismbid"));
		HttpSession session = request.getSession();

		Member member = (Member)session.getAttribute("user");
		
		Order order = new Order();
		order.setHismbid(hismbid);
		order.setMymbid(member.getMbid());
		
		OrderService service = new OrderService();
	
		List<Order> list = service.findOrderByMb(order);
		for(Order od :list){
			if(od.getHismbid()==hismbid){
				request.setAttribute("fail", "fail");
				RequestDispatcher rd = request
						.getRequestDispatcher("/member/HisRecipe?hismbid="+hismbid);
				rd.forward(request, response);
				return;
				
				
			}
		}
		
		service.insertOrder(order);
		request.setAttribute("success", "success");
		RequestDispatcher rd = request
				.getRequestDispatcher("/member/HisRecipe?hismbid="+hismbid);
		rd.forward(request, response);
		return;
		
		
		}
		catch(Exception e){
			

			RequestDispatcher rd = request
					.getRequestDispatcher("../index.jsp");
			rd.forward(request, response);
			return;
		}
		
		
		
		
	}
	

}
